<?php

namespace App\Admin\Controllers;

use App\Models\Device;
use App\Models\VisitWebTask;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class VisitWebTaskController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '网页访问';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new VisitWebTask());

        $grid->column('id', __('Id'));
//        $grid->column('task_id', __('Task id'));
//        $grid->column('phone_number', __('Phone number'));
        $grid->column('device_id', __('Device id'));//->filter(Device::all()->pluck('full_tag','id'));
        $grid->column('agent_code', __('调用者'));
        $grid->column('file', __('File'))->image('/storage/');
        $grid->column('final_url', __('Final url'))->link();
        $grid->column('url', __('Url'));
//        $grid->column('http_code', __('Http code'));
//        $grid->column('message', __('Message'));
        $grid->column('status', __('Status'));//->filter(VisitWebTask::STATUS_VALUE);
//        $grid->column('got_at', __('Got at'));
        $grid->column('created_at', __('Created at'))->sortable();;
//        $grid->column('updated_at', __('Updated at'));
//        $grid->column('status', __('重试'));//->action(Retry::class);
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->equal('device_id', __('Device id'))->select(Device::all()->pluck('full_tag','device_id'));
            $filter->equal('status', __("Status"))->select(VisitWebTask::STATUS_VALUE);
            $filter->equal('url', __("Url"));
            $filter->between('created_at', __('Created at'))->date();

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(VisitWebTask::findOrFail($id));

        $show->field('id', __('Id'));
//        $show->field('task_id', __('Task id'));
        $show->field('phone_number', __('Phone number'));
        $show->field('device_id', __('Device id'));
        $show->field('file', __('File'));
        $show->field('final_url', __('Final url'));
        $show->field('url', __('Url'));
        $show->field('http_code', __('Http code'));
        $show->field('message', __('Message'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new VisitWebTask());

//        $form->number('task_id', __('Task id'));
//        $form->mobile('phone_number', __('Phone number'));
        $form->select('device_id', __('Device id'))->options(Device::all()->pluck('full_tag','device_id'))->required();
//        $form->image('file', __('File'));
//        $form->url('final_url', __('Final url'));
        $form->url('url', __('Url'))->required();
//        $form->text('http_code', __('Http code'));
//        $form->text('message', __('Message'));
        $form->select('status', __('Status'))->options(VisitWebTask::STATUS_VALUE)->default(VisitWebTask::STATUS_CREATED);

        return $form;
    }
}
